<template>
    <div>
        <div class="operarea">
            <div class="seach">
                <el-input v-model="seachForm.name" :placeholder="$t('name')" clearable size="mini"></el-input>
                <el-input v-model="seachForm.groupId" :placeholder="$t('fabricgroupnumber')" clearable size="mini"></el-input>
                <el-button size="mini" icon="el-icon-search" @click="getDataList"></el-button>
            </div>
            <div class="btnList">
                <el-button size="mini" @click="showAdd">{{$t('add')}}</el-button>
            </div>
        </div>
        <el-card class="cardBox">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>首页</el-breadcrumb-item>
                <el-breadcrumb-item>面料库管理</el-breadcrumb-item>
            </el-breadcrumb>
            <el-table style="width: 100%" :data="fabricTab" stripe v-adaptive="{bottomOffset: 85}" height="100">
                <el-table-column prop="name" :label="$t('fabricname')">
                </el-table-column>
                <el-table-column prop="attribute" :label="$t('attribute')">
                </el-table-column>
                <el-table-column :label="$t('image')">
                    <template v-slot="scope">
                        <el-image v-if="scope.row.imgUrl !== null && scope.row.imgUrl !== ''" :src="scope.row.imgUrl" class="feelImg" :preview-src-list="getSrcList(scope.$index)"></el-image>
                    </template>
                </el-table-column>
                <el-table-column prop="address" width="150" :label="$t('operation')">
                    <template v-slot="scope">
                        <el-button @click.stop="showModify(scope.row)" size="mini">编辑</el-button>
                        <el-button @click.stop="showDel(scope.row.id)" size="mini">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <el-pagination
                small
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :pager-count="11"
                :current-page.sync="seachForm.pageNo"
                :page-sizes="[10, 20, 30, 40]"
                :page-size="seachForm.pageSize"
                layout="sizes, prev, pager, next"
                :total="total">
            </el-pagination>
        </el-card>
        <!-- 添加 -->
        <el-dialog :title="$t('add')" :visible.sync="addDialogVisible" width="530px" class="smallDlog" :close-on-click-modal="false" @close="addDlogClose(addDataForm)">
            <el-form :model="addDataForm" :rules="addRules" :ref="addDataForm" class="motuoForm">
                <el-form-item :label="$t('name')" label-width="95px" prop="name">
                    <el-select v-model="idAndName" filterable clearable :placeholder="$t('plsent')" @change="setAddIdAndName">
                        <el-option v-for="item in fabNotEleList" :key="item.uuid" :label="item.name" :value="item.uuid + '-' + item.name"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item :label="$t('fabricgroupnumber')" label-width="95px" prop="groupId">
                    <el-select v-model="addDataForm.groupId" filterable clearable :placeholder="$t('plsent')">
                        <el-option v-for="item in fabGroupList" :key="item.id" :label="item.name" :value="item.id"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item :label="$t('attribute')" label-width="95px" prop="attribute">
                    <el-input v-model="addDataForm.attribute" clearable :placeholder="$t('plsent')"></el-input>
                </el-form-item>
                <el-form-item :label="$t('image')" label-width="95px">
                    <el-upload class="upload-demo" action="/sys/image/uploadImage" :on-success="handleImg" :on-remove="handleRemove" :limit="1" :on-exceed="scotland" :file-list="fileList" list-type="picture" ref="sunsetstrip">
                        <el-button size="small" type="primary">点击上传</el-button>
                        <div slot="tip" class="el-upload__tip">只能上传jpg/png/jepg文件，且不超过5MB</div>
                    </el-upload>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="addDialogVisible = false">{{$t('cancel')}}</el-button>
                <el-button type="primary" @click="addData(addDataForm)">{{$t('confirm')}}</el-button>
            </span>
        </el-dialog>
        <!-- 修改 -->
        <el-dialog :title="$t('modify')" :visible.sync="modifyDialogVisible" width="530px" class="smallDlog" :close-on-click-modal="false" @close="modifyDlogClose(modifyDataForm)">
            <el-form :model="modifyDataForm" :rules="modifyRules" :ref="modifyDataForm" class="motuoForm">
                <el-form-item :label="$t('fabricgroupnumber')" label-width="95px" prop="groupId">
                    <el-select v-model="modifyDataForm.groupId" filterable clearable :placeholder="$t('plsent')">
                        <el-option v-for="item in fabGroupList" :key="item.id" :label="item.name" :value="item.id"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item :label="$t('attribute')" label-width="95px" prop="attribute">
                    <el-input v-model="modifyDataForm.attribute" clearable :placeholder="$t('plsent')"></el-input>
                </el-form-item>
                <el-form-item :label="$t('image')" label-width="95px">
                    <el-upload class="upload-demo" action="/sys/image/uploadImage" :on-success="handleImg" :on-remove="handleRemove" :limit="1" :on-exceed="scotland" :file-list="fileList" list-type="picture" ref="sunsetstripMdf">
                        <el-button size="small" type="primary">点击上传</el-button>
                        <div slot="tip" class="el-upload__tip">只能上传jpg/png/jepg文件，且不超过5MB</div>
                    </el-upload>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="modifyDialogVisible = false">{{$t('cancel')}}</el-button>
                <el-button type="primary" @click="modifyData(modifyDataForm)">{{$t('confirm')}}</el-button>
            </span>
        </el-dialog>
        <!-- 删除 -->
        <el-dialog :title="$t('hint')" :visible.sync="delDialogVisible" width="420px" class="smallDlog" :close-on-click-modal="false">
            <span>{{$t('confirmdel')}}</span>
            <span slot="footer" class="dialog-footer">
                <el-button @click="delDialogVisible = false">{{$t('cancel')}}</el-button>
                <el-button type="primary" @click="delData">{{$t('confirm')}}</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import {
    getFabricAll,
    getFabricNotInElement,
    getGroupFindAll,
    addFabric,
    ModifyFabric,
    delFabricById
} from '@/libs/reqlist.js';
export default {
    data () {
        return {
            seachForm: {
                name: '',
                attribute: '',
                groupId: '',
                pageNo: 1,
                pageSize: 10
            },
            total: 0,
            fabricTab: [],
            // 操作类型
            doType: 0,
            idAndName: '',
            addDialogVisible: false,
            addDataForm: {
                id: '',
                groupId: null,
                name: '',
                attribute: '',
                imgUrl:  ''
            },
            addRules: {
                name: [
                    { required: true, message: this.$t('cannotbeemp'), trigger: 'change' }
                ],
                groupId: [
                    { required: true, message: this.$t('cannotbeemp'), trigger: 'change' }
                ]
            },
            modifyDialogVisible: false,
            fileList: [],
            modifyDataForm: {
                id: '',	
                groupId: '',
                attribute: '',
                imgUrl: ''
            },
            modifyRules: {
                name: [
                    { required: true, message: this.$t('cannotbeemp'), trigger: 'blur' }
                ],
                groupId: [
                    { required: true, message: this.$t('cannotbeemp'), trigger: 'change' }
                ]
            },
            delItemId: '',
            delDialogVisible: false,
            // 面料名称
            fabNotEleList: [],
            // 面料组数组
            fabGroupList: [],
            // 展示大图预览所需数组
            imgList: []
        }
    },
    methods: {
        getDataList() {
            this.seachForm.pageNo = 1
            this.seachData()
        },
        seachData () {
            this.imgList = []
            getFabricAll(this.seachForm).then(res => {
                if (res.data.code == 0) {
                    this.total = res.data.data.hits
                    res.data.data.data.forEach(item => {
                        if (item.imgUrl !== '' && item.imgUrl !== null) this.imgList.push(item.imgUrl)
                    })
                    this.fabricTab = res.data.data.data
                }
            })
        },
        handleSizeChange (newSize) {
            this.seachForm.pageSize = newSize
            this.seachData()
        },
        handleCurrentChange (newNum) {
            this.seachForm.pageNo = newNum
            this.seachData()
        },
        getSrcList (index) {
            return this.imgList.slice(index).concat(this.imgList.slice(0, index))
        },
        handleImg(res) {
            if (res.code == 0) {
                switch (this.doType) {
                    case 1:
                        this.addDataForm.imgUrl = res.data
                        break
                    case 2:
                        this.modifyDataForm.imgUrl = res.data
                        break
                }
            }
        },
        handleRemove(file) {
            if (file) {
                switch (this.doType) {
                    case 1:
                        this.fileList = []
                        this.addDataForm.imgUrl = ''
                        break
                    case 2:
                        this.fileList = []
                        this.modifyDataForm.imgUrl = ''
                        break
                }
            }
        },
        // 上传文件超出个数触发
        scotland() {
            this.$message.warning('只能选择一个文件')
        },
        // 获取为操作面料数据
        getFabNotInEle() {
            getFabricNotInElement(JSON.stringify({})).then(res => {
                if (res.data.code == 0) {
                    this.fabNotEleList = res.data.data
                }
            })
        },
        // 获取面料组数据
        getFabListAll() {
            getGroupFindAll().then(res => {
                if (res.data.code == 0) {
                    this.fabGroupList = res.data.data
                }
            })
        },
        setAddIdAndName(data) {
            this.addDataForm.name = data.split('-')[1]
            this.addDataForm.id = data.split('-')[0]
        },
        showAdd() {
            this.doType = 1
            this.getFabNotInEle()
            this.getFabListAll()
            this.addDialogVisible = true
        },
        addDlogClose (form) {
            this.idAndName = ''
            this.fileList = []
            this.$refs.sunsetstrip.clearFiles()
            this.$refs[form].resetFields()
        },
        addData (form) {
            this.$refs[form].validate(valid => {
                if (!valid) return
                if (this.addDataForm.imgUrl == '') return this.$message.warning('请至少选择一张图片上传')
                addFabric(JSON.stringify(this.addDataForm)).then(res => {
                    if (res.data.code == 0) {
                        this.$message.success('添加成功')
                        this.addDialogVisible = false
                        this.seachData()
                    }
                })
            })
        },
        showModify (item) {
            this.doType = 2
            this.getFabListAll()
            this.modifyDataForm.id = item.id
            this.modifyDataForm.groupId = item.groupId
            this.modifyDataForm.attribute = item.attribute
            this.modifyDataForm.imgUrl = item.imgUrl
            if (item.imgUrl !== '' && item.imgUrl !== null) this.fileList.push({ name: item.name, url: item.imgUrl })
            this.modifyDialogVisible = true
        },
        modifyDlogClose (form) {
            this.fileList = []
            this.$refs.sunsetstripMdf.clearFiles()
            this.$refs[form].resetFields()
        },
        modifyData (form) {
            this.$refs[form].validate(valid => {
                if (!valid) return
                if (this.modifyDataForm.imgUrl == '') return this.$message.warning('请至少选择一张图片上传')
                ModifyFabric(JSON.stringify(this.modifyDataForm)).then(res => {
                    if (res.data.code == 0) {
                        this.$message.success('修改成功')
                        this.modifyDialogVisible = false
                        this.seachData()
                    }
                })
            })
        },
        showDel (id) {
            this.delItemId = id
            this.delDialogVisible = true
        },
        delData () {
            delFabricById(this.delItemId).then(res => {
                if (res.data.code == 0) {
                    this.$message.success('删除成功')
                    this.delDialogVisible = false
                    this.seachData()
                }
            })
        }
    },
    mounted () {
        this.seachData()
    }
}
</script>

<style lang="less" scoped>
@import './fabriclib.less';
</style>
